package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.domain.Permission;

import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IPermissionService;

import cn.wolfcode.ssm.util.RequireAnnotation;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class permissionController {


    @Autowired
    private IPermissionService permissionService;

    @RequireAnnotation(name = "权限页面",expression = "permission:list")
    @RequestMapping("list")
    public String listAll(Model model, QueryObject qo){
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "permission/list";
    }


    @RequireAnnotation(name = "权限加载",expression = "permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(Model model) {
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"重新加载失败");
        }
    }


}
